package com.hanson.api;

import com.alibaba.fastjson.JSONObject;
import com.hanson.common.bean.ResultMessage;
import com.hanson.common.util.BusinessUtil;
import com.hanson.entity.GoodsCommentBean;
import com.hanson.service.GoodsCommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@Api(tags = "商品评论管理")
@RestController
@RequestMapping("/api/comment")
public class CommentApiController {
    @Autowired
    private GoodsCommentService goodsCommentService;

    @ApiOperation(value = "新增商品评论")
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public ResultMessage add(@RequestBody GoodsCommentBean goodsCommentBean, HttpServletRequest request) {
        String openId = BusinessUtil.getHeaderOpenId(request);
        goodsCommentService.addComment(goodsCommentBean, openId);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setMsg("添加评论成功！");
        return resultMessage;
    }

    @ApiOperation(value = "查询商品评论列表", notes = "scoreType: 1全部评论 2好评 3中评")
    @RequestMapping(value = "/commentList/{scoreType}/{goodsId}", method = RequestMethod.GET)
    public ResultMessage commentList(@PathVariable String scoreType, @PathVariable String goodsId) throws Exception {
        ModelMap modelMap = new ModelMap();
        List<GoodsCommentBean> list = goodsCommentService.selectByGoodsId(Integer.parseInt(goodsId), Integer.parseInt(scoreType), modelMap);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setMsg("修改成功！");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("countData", modelMap.get("countData"));
        jsonObject.put("data", list);
        resultMessage.setData(jsonObject);
        return resultMessage;
    }
}
